package com.sbs.gaoxinqutoilet.adapter;

import android.view.View;
import android.widget.TextView;

import com.sbs.gaoxinqutoilet.interfaces.StellarListener;
import com.sbs.gaoxinqutoilet.myapp.MyApplication;
import com.sbs.gaoxinqutoilet.utils.UiUtils;
import com.sbs.gaoxinqutoilet.view.stellarmap.StellarMap;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;

/**
 * Created by 聪明一只哈 on 2018/6/4.
 * Time: 2018/6/4  8:33
 */

public class StellarMapAdapter extends StellarMap.Adapter {

    private HashMap<Integer, ArrayList<String>> groups;
    private Random random;

//    public StellarMapAdapter(HashMap<Integer, ArrayList<String>> groups) {
//        this.groups = groups;
//        random = new Random();
//    }
    public StellarMapAdapter() {
        groups=new HashMap<>();
        random = new Random();
    }

    public void addData(HashMap<Integer, ArrayList<String>> groups){
        this.groups.clear();
        this.groups.putAll(groups);
    }
    /**
     * 获取动画组数量
     */
    @Override
    public int getGroupCount() {
        return groups.size();

    }

    /**
     * 获取指定组的数据的数量
     */
    @Override
    public int getCount(int groupIndex) {
        return groups.get(groupIndex).size();
    }

    /**
     * 获取显示到界面的View
     */
    @Override
    public View getView(int groupIndex, int position, View convertView) {
        ArrayList<String> oneGroup = groups.get(groupIndex);    // 取出指定的组
        final String string = oneGroup.get(position);        // 取出要显示的内容

        // 创建TextView展示要显示的内容
        TextView textView = new TextView(MyApplication.getApplicationIntstance());

        // 设置字体大小
        float size = 12 + random.nextInt(16); // 12 ~ 27
        textView.setTextSize(size);

        // 设置字体颜色
        int color = UiUtils.createRandomColor();
        textView.setTextColor(color);

        textView.setText(string);
        textView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                UiUtils.showToast(string);
                if (stellistener!=null){
                    stellistener.show(string);
                }
            }
        });

        return textView;
    }


    public void setStellarListener(StellarListener stellistener){
        this.stellistener=stellistener;
    }

    private StellarListener stellistener;


}
